package com.exe.rege.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exe.rege.common.CustomException;
import com.exe.rege.common.R;
import com.exe.rege.dto.DishDto;
import com.exe.rege.entity.DishEntity;
import com.exe.rege.service.CategoryService;
import com.exe.rege.service.DishFlavorService;
import com.exe.rege.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info("封装的数据:{}",dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
    * 菜品分页查询
    * */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<DishDto> dishDtoPage =dishService.getPage(page, pageSize, name);

        //加入records后，返回
        return R.success(dishDtoPage);
    }

    /**
     * 根据id查询菜品信息和对应口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto =dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("封装的数据:{}",dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    /**
     * 删除菜品信息
     * 根据id删除菜品，如果菜品和套餐有关联不允许直接删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        log.info("删除菜品的id：{}", ids);
        if (ids.isEmpty()){
            return R.error("此菜品已被删除");
        }
        dishService.deleteByIds(ids);
        return R.success("删除菜品成功");
    }

    /**
     * 更新菜品状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, String ids){
        log.info("改变id为{}的菜品状态为{}", ids, status);
        if (StringUtils.isEmpty(ids)){
            return R.error("此菜品已被删除");
        }
        dishService.updateStatus(status, ids);
        return R.success("菜品状态更新成功");
    }

    /**
     * 根据条件查询菜品
     * @param dishEntity //直接返回整个对象，更具有复用性，虽然这里只要分类id
     * @return
     */
    @GetMapping("/list")
    public R<List<DishEntity>> list(DishEntity dishEntity){
        //构造查询条件
        LambdaQueryWrapper<DishEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dishEntity.getCategoryId() != null, DishEntity::getCategoryId, dishEntity.getCategoryId());
        //只查询启用的菜品
        queryWrapper.eq(DishEntity::getStatus, 1);
        queryWrapper.orderByAsc(DishEntity::getSort).orderByDesc(DishEntity::getUpdateTime);

        List<DishEntity> list = dishService.list(queryWrapper);
        return R.success(list);
    }
}
